package com.gitee.wsl.reflect

import com.gitee.wsl.serialization.reflect.ReflectMethod
import com.gitee.wsl.text.template.Placeholder


object JVMReflectMethod : ReflectMethod {

    override fun findValue(obj: Any, name: String): Any? {
        if (obj is Placeholder) {
            val value = obj.getPlaceholder(name)
            if (value != ReflectMethod.NotFound) return value
        }
        val result1 = obj.r_GetField(name)
        if (result1 != ReflectResult.Missing) return result1.value

        val result2 = obj.r_CallMethod(name.asCamelCaseGetter())
        if (result2 != ReflectResult.Missing) return result2.value

        val result3 = obj.r_CallMethod(name)
        if (result3 != ReflectResult.Missing) return result3.value

        return ReflectMethod.NotFound
    }
}


internal  fun String.asCamelCaseGetter() =
    if (this.isEmpty()) {
        "get"
    } else {
        "get${this[0].uppercase()}${this.substring(1)}"
    }